define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
		$scope.form = {};
		var currentNode = null;
		var menuTreeObj;
		var tId = "";

		//单击
		var zTreeOnClick = function(event, treeId, treeNode) {
			$scope.dept_name = treeNode.name;
			currentNode = treeNode;
			tId = currentNode.tId;

			$scope.findrole(treeNode);
			$scope.$apply();
		};
		function addDiyDom(treeId, treeNode) {
			var spanObj = $("#" + treeNode.tId + "_span");

			//删除
			var deleteBtn = '<button class="btn-outline-danger btn-sm" id="deleteOrg_' + treeNode.tId + '" style="margin-left: 5px;">删除</button>';
			spanObj.after(deleteBtn);
			var deleteBtnObj = $("#deleteOrg_" + treeNode.tId);
			if (deleteBtnObj) {
				deleteBtnObj.bind("click", function() {
					$scope.deleterole(treeNode.id);
				});
			}
			// 添加子部门
			var addBtn = '<button class="btn-outline-primary btn-sm" id="addOrg_' + treeNode.tId + '" >子部门</button>';
			spanObj.after(addBtn);
			var addBtnObj = $("#addOrg_" + treeNode.tId);
			if (addBtnObj) {
				addBtnObj.bind("click", function() {
					var type = parseInt(treeNode.type) +1;
					$scope.addroleG(treeNode.id,type);
				});
			}
		}
		// zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
		var setting = {
			data: {
				simpleData: {
					enable: true
				}
			},
			check: {
				enable: false
			},
			edit: {
				enable: true
			},
			view:{
				addDiyDom: addDiyDom,
				showIcon: false,
				selectedMulti: false
			},
			callback: {
				onClick: zTreeOnClick
			}
		};
		//学校部门
		$scope.findrolegroup = function(){
			var zNodes =  [];
			httpService.post(app.urls.role_group_findGroupTree, {}, function(data){
				if("0000" === data.code) {
					zNodes = data.data;
					menuTreeObj = $.fn.zTree.init($("#groupTree"), setting, zNodes);
					$scope.$apply();

					var nodes = menuTreeObj.getNodes();
					if (nodes.length > 0) {
						currentNode = nodes[0];
						menuTreeObj.selectNode(nodes[0]);

						menuTreeObj.expandAll(true);
						setting.callback.onClick(null, menuTreeObj.setting.treeId, nodes[0]);
					}

				}else{
					eventBusService.publish("modal.alert", {"content": data.msg});
				}
			});
		}
		$scope.findrolegroup();

		$scope.findunit = function(){
			//查询所有单位
			httpService.post(app.urls.unit_selectUnit,{},function(data){
				if("0000" === data.code){
					$scope.unitList = data.data;
				}else{
					alert(data.msg);
				}
			});
		}
		$scope.findunit();

		$scope.findrole = function(row){
			$scope.groupt = row;
			$scope.form.fk_group=row.id;
			httpService.post(app.urls.role_selectByType,$scope.form,function(data){
				if("0000" === data.code){
					$scope.roles = data.data;
					$scope.checkMenu($scope.roles[0],$scope.roles[0].type);
				}else{
					alert(data.msg);
				}
			});
		}

		//获取具体用户数据
		$scope.checkMenu = function(row,type){
			$scope.show="true";
			$scope.fk_role=row.id;
			$scope.form.fk_role=row.id;
			$scope.row_name=row.name;
			$scope.fk_group=row.fk_group;
			$scope.type=type;

			if($scope.fk_group==undefined){
				$("#"+$scope.controllerName + "_th").addClass("fe-folder" );
				$("#"+$scope.controllerName + "_th").removeClass("fe-user");
				$("#"+$scope.controllerName + "_roleG").attr("style","display:");
				$("#"+$scope.controllerName + "_role").attr("style","display:none;");
				$("#"+$scope.controllerName + "_user_stu").attr("style","display:none;");
				$("#"+$scope.controllerName + "_user_tea").attr("style","display:none;");
			}else{
				if(type == '0'){
					$("#"+$scope.controllerName + "_th").addClass("fe-folder" );
					$("#"+$scope.controllerName + "_th").removeClass("fe-user");
					$("#"+$scope.controllerName + "_roleG").attr("style","display:");
					$("#"+$scope.controllerName + "_role").attr("style","display:none;");
					$("#"+$scope.controllerName + "_user_stu").attr("style","display:none;");
					$("#"+$scope.controllerName + "_user_tea").attr("style","display:;");
				}else if(type == '3'){
					$("#"+$scope.controllerName + "_th").addClass("fe-user" );
					$("#"+$scope.controllerName + "_th").removeClass("fe-folder");
					$("#"+$scope.controllerName + "_roleG").attr("style","display:none;");
					$("#"+$scope.controllerName + "_role").attr("style","display:");
					$("#"+$scope.controllerName + "_user_stu").attr("style","display:");
					$("#"+$scope.controllerName + "_user_tea").attr("style","display:none;");
				}else{
					$("#"+$scope.controllerName + "_th").addClass("fe-user" );
					$("#"+$scope.controllerName + "_th").removeClass("fe-folder");
					$("#"+$scope.controllerName + "_roleG").attr("style","display:none;");
					$("#"+$scope.controllerName + "_role").attr("style","display:");
					$("#"+$scope.controllerName + "_user_tea").attr("style","display:");
					$("#"+$scope.controllerName + "_user_stu").attr("style","display:none;");
				}
			}
			if($scope.fk_group == 'd5c31570-550e-4135-8fa9-a6d5dbde3c3b' || $scope.fk_role == 'd5c31570-550e-4135-8fa9-a6d5dbde3c3b' ){
				$("#"+$scope.controllerName + "_button0").attr("disabled","true");
				$("#"+$scope.controllerName + "_button1").attr("disabled","true");
			}else{
				$("#"+$scope.controllerName + "_button0").removeAttr("disabled");
				$("#"+$scope.controllerName + "_button1").removeAttr("disabled");
			}
			$scope.find();
		}

		$scope.find = function() {
			httpService.postpage(app.urls.role_selectUserRole,{"id":$scope.form.fk_role,"fk_unit":$scope.form.fk_unit,"id_crad":$scope.form.id_crad,"name":$scope.form.name},function(data){
				if("0000" === data.code){
					$scope.rows = data.data;
				}else{
					alert(data.msg);
				}
			});
		}

		//接收数据刷新
		eventBusService.subscribe('content.page.reload', function(event,data) {
			$scope.find();
		});
		//接收数据刷新
		eventBusService.subscribe('content.pageRole.reload', function(event,data) {
			$scope.groupsid = data;
			$scope.findrolegroup(1);
		});
		//接收数据刷新
		eventBusService.subscribe('content.pageGroup.reload', function(event,data) {
			$scope.rolesid = data;
			$scope.findrole($scope.groupt,1);
		});

		//按钮点击事件-条件查询
		$scope.select = function(){
			$scope.page.current = 1;
			$scope.find();
		}


		//按钮点击事件-添加分组
		$scope.addroleG = function(row,type){
			eventBusService.publish("modal.page","modules/content/usermanagement/role/addRoleG?father_id="+row+"&type="+type);
		}

		//按钮点击事件-添加角色
		$scope.addrole = function(fk_group){
			eventBusService.publish("modal.page","modules/content/usermanagement/role/addRole?fk_group="+fk_group);
		}

		//修改角色
		$scope.updateRule= function(fk_role,fk_group,type) {
			if(type == '0'){
				eventBusService.publish("modal.page","modules/content/usermanagement/role/updateRoleG?fk_role="+fk_role+"&fk_group="+fk_group);
			}else{
				eventBusService.publish("modal.page","modules/content/usermanagement/role/updateRole?fk_role="+fk_role+"&fk_group="+fk_group);
			}
		}

		//按钮点击事件-添加用户
		$scope.add = function(fk_role,type){
			eventBusService.publish("modal.page","modules/content/usermanagement/role/addUser?fk_role="+fk_role+"&type="+type+"&fk_group="+$scope.fk_group);
		}

		//按钮点击事件-添加学生
		$scope.addstudent = function(fk_role,type){
			eventBusService.publish("modal.page","modules/content/usermanagement/role/addStudent?fk_role="+fk_role+"&type="+type+"&fk_group="+$scope.fk_group);
		}

		//按钮点击事件-移除用户
		$scope.delete = function(){
			var values = [];
			var ids="";
			$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
				values.push($(this).val());
				ids = ids+",'"+$(this).val()+"'";
			});

			if(values.length < 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择您要删除数据。"});
			}else{
				eventBusService.publish("modal.alert",{"content":"你是否确认删除这"+values.length+"条数据？","isAutoClose":false,"confirm":function(){
						httpService.post(app.urls.role_deleteUserRole,{"ids":ids.replace(',',''),"fk_role":$scope.fk_role},function(data){
							if("0000" === data.code){
								$scope.select();
							}else{
								alert(data.msg);
							}
						});
					},"cancel":function(){
						//用户取消，不做操作
					}});


			}
		}

		//按钮点击事件-移除分组
		$scope.deleterole = function(row){
			eventBusService.publish("modal.alert",{"content":"你是否确认删除？","isAutoClose":false,"confirm":function(){
					httpService.post(app.urls.role_selectByGroup,{"fk_group":row,"unitId":$scope.form.unitId},function(data){
						if("0000" === data.code){
							if(data.data.length == 0){
								httpService.post(app.urls.role_group_delete,{"ids":row},function(data){
									if("0000" === data.code){
										$scope.findrolegroup(0);
									}else{
										alert(data.msg);
									}
								});
							}else{
								alert("该分组下有角色！");
							}
						}else{
							alert(data.msg);
						}
					});
				},"cancel":function(){
					//用户取消，不做操作
				}});
		}


		//按钮点击事件-移除角色
		$scope.deleteroles = function(row){
			eventBusService.publish("modal.alert",{"content":"你是否确认删除？","isAutoClose":false,"confirm":function(){
					httpService.post(app.urls.role_selectUserRole,{"id":row,"unitId":$scope.form.unitId},function(data){
						if("0000" === data.code){
							if(data.data.length == 0){
								httpService.post(app.urls.role_delete,{"ids":row},function(data){
									if("0000" === data.code){
										$scope.findrole($scope.groupt,0);
									}else{
										alert(data.msg);
									}
								});
							}else{
								alert("该角色下有用户!");
							}
						}else{
							alert(data.msg);
						}
					});
				},"cancel":function(){
					//用户取消，不做操作
				}});
		}

		$scope.showul=function(num){
			$(".gzdw").removeClass("ss-display");
		}

		$scope.getSchool=function(school,num){
			if(school == 0){
				$scope.form.fk_unit = "";
				$scope.gzdwname = "";
				$(".gzdw").addClass("ss-display");
			}else{
				$scope.form.fk_unit = school.id;
				$scope.gzdwname = school.name;
				$(".gzdw").addClass("ss-display");
			}
			$scope.find();
		}
		//选择数据
		$scope.optData = function (element) {
			if($(element).find("input[name='dataPk']").is(":checked")){
				$(element).find("input[name='dataPk']").prop("checked",false);
				$(element).removeClass("opt_tr");
			}else{
				$(element).find("input[name='dataPk']").prop("checked",true);
				$(element).addClass("opt_tr");
			}
		}
		$scope.checkAlls = function(target) {
			$("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
				if ($(this).prop("checked")) {
					$(this).prop('checked', false);
					$(this).parents("tr").removeClass("opt_tr");
				} else {
					$(this).prop('checked', true);
					$(this).parents("tr").addClass("opt_tr");
				}
			})
		}
	}}});